package com.gxa.hotel.portal.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@RestController
@Slf4j
@Api
public class PayController {

   private static final  String serverUrl="http://openapi.alipaydev.com/gateway.do";
   private static final  String APP_ID="2021000121621856";
   private static final String APP_PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCRHXbirWdLA13d3Hwaeq2ukQDK00y9iED4dxLmHcq+PVI4Yf6Hw4w69R+NvTNDg7o10PFrru8HVzqbBrxOB9NBNHgLatFXoQ2YrvJQdcz/+rkdRZ14Vg0XgtQ3ZQ7CMJe/4tD4cRbRopZeo0W5nYYaLLqGHeyX9thcaDoWTn9mIcpjVS+i+exRJYsy0IME8MLKiMG/OcKOjGpq9WTLQFZDakB93RtEfa3BiRonBeOop+v8L/77OAfQeDVX0WChahZU7YxtkwXTjojKmGkZlm4mpZtBujgVw3Zlhsde85EzO9pujCuOny1T/67WtsBOLzu4prwzC9oxiFjyHWZ/VNUfAgMBAAECggEAa1uWCatqALgT6MBm3BwohD5ojKV4X7v+/RLy22XtCYNKYcY1zXEhPLj+SBlSZL9+srPK9/SqiA7Dn8+C3mOxqcrMiP3UtHTFH1FGmbodGL//1aUcE9vtwZgbsoPEQGw8yExgd30K+brEbm1ydxkQDitLL0/SKrcHaI3/c2NNo1t1FyMYWjuwr2WcHHazdswAxMEa/ad0YiT4azr61SviVoNKp37/9AXYF3xAXnvfqSa1n1zuSs9x7yiPYCLu7LTkDg/k741cjOA4Zzdibl2394iCYm3MAoQAXeIF92eKsPVSRDKinoM87vig+DamLDPX/azqXoS9UhgvBcz7JqWBqQKBgQD0hthAkaIm7+kHUTnUEC2W+jui3WFLj6Qx/dY/J4AeD9/Iztzhctw4SbGh/buFFXdISSVjKD2C1CVn/N7OyiXyDOkiuUMWHM2XYfKkGC5eHIq9HoTJqMz2/R8pGnPcxCd+QCItkXfQirkOvjXrsLX10uzhqbNR4GNXcuZ2fu1L5QKBgQCX7IcS+aVkedAqzrgMSwOD7Ar+1tmsepKxOH0an/NlTl7VNYVWTz4ZeoBABqGkOnaUTXT4cmzHmD/StfT802hQ7zPxfhs7JdMay9f9RTNgkYYBGhbXkynbUS5rxH5XkntUQjLar6ZOhh3ms1Mu2yo7DK6k3lOba7tdAnrxpqF0swKBgQDMbaeBY7+SmePRsECOX/AdVppg8H+U8kOtX6+sINPlThQjB352JNajYEjXSw9olaehkTVmjo/OPpn/M9L6QtoyYETQON4zMGVY+BQox+aL24TJ0yfkh/DDVJ6meG0A1iTGUcvBGOp4OGG6R59Aqf2VxH8xsfQusU+cHn0596dfWQKBgDYVC1ij6k7EVmiGwloN8ihqRNfHYCHYWJQiCdcKjbjIl1XLAbZh6zditnkUY0J1VXUhxbwT78l113KJN7cxAvecz57uR39czbPCURdmJQ4Bf/SwduUzDDl4ws+2WUuPZ7vs/PGAifF2KPrRZ5FMj9IJh7pFMvom9cH8egvAR8WlAoGAeE1HawI3X3YjNQUi3yPV/quGKlkQoHnBkPaXmQJ5pu4wSgvuYFfD2a3VvaMAOoIbsjsUZ0DnNaJeOlhKtYxM9yqr0ZJAQRAx9hanq9hxv0EFa+vA2RAV0Bkeckfps6rHw+q4vIFgi+qb1EjYCK5/ngWJ/8AyskAKl1aJdktqYb0=";
   private static final String FORMAT="json";
   private static final String CHARSET="GBK";
   private static final  String ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjk0yqKlPej0t3xaFKU1GCqHZwfMUk5OGdkDOtmfREMkfIogG9XQlvdsiwO0LLjUkMVMjIB4yunHECjd9DeMJwf0/MKmo2i1n5jZZLqBsmmVt7N8dz9+QqUaNlx4GcxR4+CCGuMePPh42XLeuF/y566kiPj3AbI1w2Cf3UEpfRKNwzm9hZPdQzxTq/7ZINDB/yU5Xjmm2IHPUHjeuCIOxru1RoegCxlnwTgfwlN3zzQfwSj09MjUMAKihQgW8qH+3wuTxmWXapYGPRLGOv0rCPtkdwTdqRpkZyh+QS7YeGTNdN7N35TzMDBlZY+QkLTugE7amzQ0si3j2LwUbjR18OwIDAQAB";
   private static final String SIGN_TYPE="RSA2";


    String id = UUID.randomUUID().toString().replace("-", "");
     @ApiOperation("支付")
    @RequestMapping("/pay")
    public   String   doPost (HttpServletRequest httpRequest,HttpServletResponse httpResponse)   throws ServletException, IOException {
        AlipayClient alipayClient =  new DefaultAlipayClient( serverUrl , APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);  //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request
       //只是给内部的jsp跳转使用
        // alipayRequest.setReturnUrl( "http://localhost:8081/payreturn" );

        alipayRequest.setNotifyUrl( "http://1.117.167.227:8081/payreturn" ); //在公共参数中设置回跳和通知地址
         String money = httpRequest.getParameter("money");
         String name = httpRequest.getParameter("name");

         alipayRequest.setBizContent( "{"  +
               "    \"out_trade_no\":\""+id+"\","  +
               // "    \"out_trade_no\":\"1515151515151515\","  +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
//                "    \"total_amount\":88.88,"  +
                "    \"total_amount\":"+money+","  +
                "    \"subject\":\""+name+"\","  +
                "    \"body\":\""+name+"\","  +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
                "    \"extend_params\":{"  +
                "    \"sys_service_provider_id\":\"2088511833207846\""  +
                "    }" +
                "  }" ); //填充业务参数
        String form= "" ;
        try  {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
        }  catch  (AlipayApiException e) {
            log.error(e.getErrMsg());
        }
        return form;
    }

    @RequestMapping("/payreturn")
    public Map payReturn(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        Iterator<Map.Entry<String, String[]>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String[]> next = iterator.next();
            log.info("key:{},value:{}",next.getKey(),next.getValue());
        }
        return null;
    }
  @RequestMapping("/payback")
    public Boolean payBack(HttpServletRequest req,HttpServletResponse res) throws AlipayApiException {
      AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,APP_ID,APP_PRIVATE_KEY,FORMAT,CHARSET,ALIPAY_PUBLIC_KEY,SIGN_TYPE);
      String id1="60c3db8feb004bb8a8751852d02c15bf";
      AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
      String orn = UUID.randomUUID().toString().replace("-", "");
     // String  id1="cdb594e060f14192812968d5943f9885";
      request.setBizContent("{" +

             "    \"out_trade_no\":\""+id1+"\"," +
             // "    \"out_trade_no\":\"1515151515151515\"," +

            "    \"trade_no\":\"2022071322001413780502222592\"," +

              "    \"refund_amount\":88.88," +

              "    \"refund_reason\":\"正常退款\"," +

              "    \"out_request_no\":\""+orn+"\"," +

              "    \"operator_id\":\"OP001\"," +

              "    \"store_id\":\"NJ_S_001\"," +

              "    \"terminal_id\":\"NJ_T_001\"" +

              "  }");

      AlipayTradeRefundResponse response = alipayClient.execute(request);

//      if(response.isSuccess()){
//
//          //System.out.println("调用成功");
//          return
//
//      } else {
//
//          System.out.println("调用失败");
//
//      }
     return response.isSuccess();
}
}
